const service = require('../../utils/service.js')
const api = require('../../utils/util.js')
Page({
  data: {
    navIndex: 0,
    shopInfo: {},
    userInfo: {},
    page: 1,
    orderList: [],
    hasMoreData: true,
    cancelReason: '',
    showOther: false,
    reason_id: '',
    otherReasonText: '',
    showPon: false,
    order_id: '',
    noData: false,
    
  },
	onShow: function(){
		let shopInfo = wx.getStorageSync('shopInfo')
    let userInfo = wx.getStorageSync('shop_data')
    this.setData({
      shopInfo: shopInfo,
      userInfo: userInfo
    })
    this.init()
	},
  onLoad: function (options) {
  	if (options.type) {
      this.setData({
        navIndex: options.type
      })
    }
  },

  init: function () {
    this.setData({
      page: 1,
      orderList: [],
      hasMoreData: true
    })
    this.getOrderList()
    this.getCancelReasonList()
  }, 

  // 获取订单列表
  getOrderList: function () {
    wx.showLoading({
      title: '正在加载',
    })
    let that = this
    api.ajax(service.url('getOrderList'), {
      
      order_state: that.data.navIndex,
      page: that.data.page
    },
    function (res) {
      if (res.code == 0) {
        if (res.data.length > 0) {
          for (var i = 0, len = res.data.length; i < len; i++) {
            that.data.orderList.push(res.data[i])
          }
          that.setData({
            hasMoreData: true,
            orderList: that.data.orderList
          })
        } else {
          that.setData({
            hasMoreData: false
          })
        }
        if (that.data.orderList.length <= 0) {
          that.setData({
            noData: true
          })
        } else {
          that.setData({
            noData: false
          })
        }
        wx.hideLoading()
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      } else {
        api.toast(res.message)
      }
    })
  },

  showLos: function (e) {
    let id = e.currentTarget.dataset.id
    this.setData({
      showPon: true,
      order_id: id,
      reason_id: ''
    })
  },

  hideLos: function(){
    this.setData({
      showPon: false,
      order_id: ''
    })
  },

  //阻止穿透
  move: function () {

  },

  // 取消订单原因
  getCancelReasonList: function () {
    let that = this
    api.ajax(service.url('getCancelReasonList'), {}, function (res) {
      that.setData({
        cancelReason: res
      })
    })
  },

  // 选择取消原因
  radioChange: function (e) {
    let select = e.detail.value
    this.setData({
      otherReasonText: ''
    })

    if (select == 3) {
      this.setData({
        showOther: true,
        reason_id: select
      })
    } else if (select == 1) {
      this.setData({
        showOther: false,
        reason_id: select,
        otherReasonText: '不想要了'
      })
    } else if (select) {
      this.setData({
        showOther: false,
        reason_id: select,
        otherReasonText: '拍错了'
      })
    }
  },

  // 获取其他理由
  getOther: function (e) {
    let str = e.detail.value
    this.setData({
      otherReasonText: str
    })
  },

  // 取消订单
  cancelOrder: function (e) {
    this.setData({
      showPon: false
    })
    let that = this
    let id = this.data.order_id
    if (this.data.reason_id == '' || this.data.reason_id == null) {
      api.toast('请选择取消原因')
      return
    }
    let other_reason = this.data.otherReasonText
    if (other_reason == '' || other_reason == null) {
      api.toast('请填写理由')
      return;
    }
    wx.showModal({
      title: '提示',
      content: '确定取消该订单?',
      success: (rel) => {
        if (rel.confirm) {
          api.ajax(service.url('cancelOrder'), {
            order_id: id,
            order_reason_id: that.data.reason_id,
            other_reason: other_reason
          },
          function (res) {
            if (res.code == 0) {
              that.setData({
                hasMoreData: true,
                page: 1,
                orderList: []
              })
              api.toast('取消订单成功')
              setTimeout(() => {
                that.getOrderList()
              } ,1000)
            } else {
              api.toast(res.message)
            }
          })
        }
      }
    })
  },
  // 确认付款
  payOrder: function (e) {
    let money = e.currentTarget.dataset.money
    let pay_key = e.currentTarget.dataset.key
    wx.navigateTo({
      url: `../pay/pay?pay_key=${pay_key}&money=${money}`,
    })
  },

  // 申请退款
  refunApply: function (e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `../refun_apply/refun_apply?order_id=${id}`,
    })
  },

  // 提醒发货
  remindSend: function (e) {
    let that = this
    let id = e.currentTarget.dataset.id
    api.ajax(service.url('reminderSlip'), {
      
      order_id: id
    },
    function (res) {
      console.log(res)
      if (res.code == 0) {
        api.toast('已提醒商家发货')
      } else {
        api.toast(res.message)
      }
    })
  },

  // 延长收货
  extendCollect: function (e) {
    let that = this
    let id = e.currentTarget.dataset.id
    api.ajax(service.url('setCustomerDelay'), {
      
      order_id: id
    },
    function (res) {
      console.log(res)
      if (res.code == 0) {
        api.toast('已延长收货时间')
      } else {
        api.toast(res.message)
      }
    })
  },

  // 确认收货
  confirmCollect: function (e) {
    let that = this
    let id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '是否收到货！',
      confirmText: '确认收货',
      success: (res) => {
        if (res.confirm) {
          api.ajax(service.url('orderReceived'), {
            
            order_id: id
          },
          function (res) {
            if (res.code == 0) {
              api.toast('已确认收货')
              that.setData({
                hasMoreData: true,
                page: 1,
                orderList: []
              })
              that.getOrderList()
            } else {
              api.toast(res.message)
            }
          })
        }
      }
    })
  },

  // 删除订单
  deleteOrder: function (e) {
    let that = this
    let id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '确认删除该订单吗？',
      success: function (res) {
        if (res.confirm) {
          api.ajax(service.url('delOrder'), {
            order_id: id
          },
          function (res) {
            if (res.code == 0) {
              api.toast('删除订单成功')
              that.setData({
                hasMoreData: true,
                page: 1,
                orderList: []
              })
              that.getOrderList()
            } else {
              api.toast(res.message)
            }
          })
        }
      }
    }) 
  },

  // 撤销申请
  revokeRefun: function (e) {
    let that = this
    let return_id = e.currentTarget.dataset.returnid
    wx.showModal({
      title: '提示',
      content: '确定撤销退款申请吗？',
      success: (rel) => {
        if (rel.confirm) {
          api.ajax(service.url('undoRefund'), {
            
            order_return_id: return_id
          },
          function (res) {
            if (res.code == 0) {
              api.toast('退款申请撤销成功')
              setTimeout(function () {
                that.setData({
                  hasMoreData: true,
                  page: 1,
                  orderList: []
                })
                that.getOrderList()
              }, 1000)
            } else {
              api.toast(res.message)
            }
          })
        }
      }
    })
  },

  // 退款详情
  refunApplyDetail: function (e) {
    console.log(e)
    let refun_id = e.currentTarget.dataset.refunid
    let order_id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `../refun_detail/refun_detail?refun_id=${refun_id}&order_id=${order_id}`,
    })
  },

  // 查看物流
  showLog: function (e) {
    let order_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `../flow/flow?order_id=${order_id}`,
    })
  },

  //去评价
  toComment:function(e){
    wx.navigateTo({
      url: '../comment_goods_order/comment_goods_order?order_id=' + e.currentTarget.dataset.id,
      success: function(res) {},
    })
  },

  // 切换列表
  switchNav: function (e) {
    var type = e.target.dataset.type
    this.setData({
      navIndex: type,
      page: 1,
      orderList: []
    })
    this.getOrderList()
  },

  // 引导下载app
  downlowApp: function () {
    wx.showModal({
      title: '提示',
      content: '下载APP更方便联系客服,是否下载',
      confirmText: '下载',
      success: (res) => {
        if (res.confirm) {
          wx.navigateTo({
            url: '../download_app/download_app',
          })
        }
      }
    })
  },


  // 前往订单详情
  toOrderDetail: function (e) {
    let order_id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `../order_detail/order_detail?order_id=${order_id}`,
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let type = this.data.navIndex
    let page = this.data.page += 1
    this.setData({
      page: page
    })
    if (this.data.hasMoreData) {
      this.getOrderList()
    } else {
      api.toast('没有更多数据了')
    }
  },

  // 邀请好友
  onShareAppMessage: function (e) {
    let goods_id = e.target.dataset.goodsid
    let img = e.target.dataset.img
    if (e.from == 'button') {
      return {
        title: '来和我一起拼团吧',
        path: 'pages/goods_detail/goods_detail?goods_id=' + goods_id,
        imageUrl: img
      }
    }
  },

  //下拉刷新
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    let type = this.data.navIndex
    this.setData({
      navIndex: type,
      page: 1,
      orderList: []
    })
    setTimeout(() => {
      this.getOrderList()
    }, 1500)
  }, 
})